<?php
set_time_limit(0);
error_reporting(E_ALL);
require_once './Classes/PHPExcel.php';
// SELECT a.* FROM `u3` a, u3 b WHERE a.id != b.id  and a.name=b.name;
$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '姓名')
            ->setCellValue('B1', '手机')
            ->setCellValue('C1', '学号')
            ->setCellValue('D1', '年级')
            ->setCellValue('E1', '区')
            ->setCellValue('F1', '学校')
            ->setCellValue('G1', '分数')
;
include 'config.php';

$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
 
$p=0;
$number=100000;
//$sql="select * from u3 where nianji='三年级' order by fen desc limit ".($p*$number).", ".$number." ";
$sql="select * from u3 order by fen desc limit ".($p*$number).", ".$number." ";

$query =$mysqli->query($sql);
$index=2;
while ($list=$query->fetch_array()){
	$list['nianji']='三年级';
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A'.$index, $list['name'])
    ->setCellValue('B'.$index, $list['phone'].' ')
    ->setCellValue('C'.$index, $list['hao'].' ')
    ->setCellValue('D'.$index, $list['nianji'])
    ->setCellValue('E'.$index, $list['qu'])
    ->setCellValue('F'.$index, $list['xuexiao'])
    ->setCellValue('G'.$index, $list['fen'])
    ;
    $index++;
}
 
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('学生');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="三年级.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
